package com.kanche.bigbaby.collection.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kanche.bigbaby.R;

import java.util.List;
import java.util.Map;

//import com.kkche.bss.R;

/**
 * @author: nealmi
 * Date: 9/27/13
 * Time: 5:12 PM
 */
public class KeyValueSpinnerAdapter extends BaseAdapter {
    private List<Map<String, String>> mList;
    private Context mContext;

    public KeyValueSpinnerAdapter(List<Map<String, String>> list, Context context) {
        mList = list;
        mContext = context;
    }

    public void refresh(List<Map<String, String>> list) {
        mList = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Map<String, String> item = (Map<String, String>) getItem(position);
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.general_spinner_item, null);

            holder.textView = (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.textView.setText(item.get("label"));
        if (item.containsKey("icon")) {
            int resourceId = Integer.parseInt(item.get("icon"));
            Drawable drawable = mContext.getResources().getDrawable(resourceId);
            Drawable d;
            if (drawable instanceof BitmapDrawable) {
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                d = new BitmapDrawable(mContext.getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));
            } else {
                d = drawable;
            }

            holder.textView.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
        }
//        ((TextView) convertView).setText(item.get("label"));
        return convertView;
    }

    class Holder {
        private TextView textView;
    }
}